/**
 * Created by JF-Z on 2017/12/8.
 */
angular.module('module.devup')
    .controller('DeviceListController', DeviceListController);
DeviceListController.$inject = ['$stateParams','$state','$http','DevupService'];
function DeviceListController($stateParams,$state, $http,DevupService) {
    var vm = this;
    var shopid = $stateParams.shopid;
    vm.pageSize =10;
    vm.currentPage = 1;
    vm.verList = [];
    vm.upLists=[];
    ///////////////////////////////////////////////////
    DevupService.getDev(shopid).$promise.then(function(data){
       /* for(var i = 0 ;i<data.length;i++){
         data[i].check = false;
         }*/
        vm.devList=data;
        version(vm.devList,0);

    })
    function version(list,i){
        if(i>=list.length){
            return;
        }
        var devlist=list[i];
        DevupService.getVer(devlist.devid).$promise.then(function(info){
            vm.verList.push(info);
            version(list,i+1)
        },function(){
            version(list,i+1)
        })

    }
//返回
    vm.back=function () {
        history.go(-1);
    }


/*选择框开始*/
    vm.selection_bind=[];//存贮选中对象数组
    var aim = document.getElementsByName('chb');//获取本页全部数据CheckBox
//选择单条数据
    vm.prepare_data=function(ver){
        vm.currentmaster = false;
        check(ver);

        console.log( vm.selection_bind)
    }
//全选数据
    vm.prepare_alldata=function(master){
        checkall(master);
        console.log( vm.selection_bind)

    }
//选择本页数据
          vm.prepare_currentall = function (currentmaster) {
        if(vm.master){
            checkall(false);
        }
        console.log(currentmaster)

        if(currentmaster){
            if(vm.selection_bind.length != 0){
                for(var i = 0;i < vm.selection_bind.length;i++){
                    for(var j = 0;j <  vm.verList.length;j++){
                        if(vm.selection_bind[i].devid == vm.ver[j].devid){
                            vm.verList[j].checked = false;
                            break;
                        }
                    }
                }
                vm.selection_bind = [];
            }
            for(var i = 0;i < aim.length;i++){
                for(var j = 0;j < vm.verList.length;j++){
                    if(aim[i].id == vm.verList[j].devid){
                        vm.verList[j].checked = true;
                        check(vm.verList[j]);
                        break;
                    }
                }
            }
        }else {
            for(var i = 0;i < aim.length;i++){
                for(var j = 0;j < vm.verList.length;j++){
                    if(aim[i].id == vm.verList[j].devid){
                        vm.verList[j].checked = false;
                        check(vm.verList[j]);
                        break;
                    }
                }
            }
            vm.selection_bind = [];
        }
        console.log( vm.selection_bind)
    }
//单独选择方法
    function check(ver) {
        vm.judge = false;
        for(var i = 0;i < vm.selection_bind.length;i++){
            if(vm.selection_bind[i].devid == ver.devid){
                vm.judge = true;
                vm.index = i;
                break;
            }
        }
        if(ver.checked && !vm.judge){
            vm.selection_bind.push(ver);
        }else if(!ver.checked && vm.judge){
            vm.selection_bind.splice(vm.index,1);
        }
        if(vm.selection_bind.length === vm.verList.length){
            vm.master = true;
        }else {
            vm.master = false;
        }
    }
//全选方法
    function checkall(master) {
        vm.selection_bind = [];
        if(master){
            angular.forEach(vm.verList,function (ver) {
                ver.checked = true
                vm.selection_bind.push(ver);
            })
        }else{
            angular.forEach(vm.verList,function (ver) {
                ver.checked = false
                vm.selection_bind = [];
            })
        }

    }
//页数更改取消本页全选
    vm.pageChangeHandler = function (newpage) {
        console.log("点击"+newpage+"页");
        //未全选则清空存贮数组
        if(vm.currentmaster){
            vm.currentmaster = false;
            vm.prepare_currentall(false)
        }
        console.log( vm.selection_bind)
    }

//批量升级
    vm.visible = false;
    vm.upBatch=function () {
        vm.visiable=!vm.visiable

    }
    /*选择框结束*/
//确定升级
     vm.updatas=function () {
        console.log(vm.selection_bind)
         for(var i=0;i<vm.selection_bind.length;i++){
             verDatas(vm.selection_bind[i]);
         }
         DevupService.getDevUp(vm.upLists).$promise.then(function (data) {
             vm.devUp=data;
             alert("上传成功")
         },function (error) {
             alert("上传失败")
         });

     };


//弹窗
    vm.verPop=function (ver) {
        $(".bgPop,.pop").show();
        verData(ver);

    }
    vm.cancel=function () {
        $(".bgPop,.pop").hide();
    }
//获取数据
    function verDatas(ver) {
        var info = {
            devid: ver.devid,
            hardv: ver.hardver,
            softv: ver.softver
        };
        var verInfo = [];
        verInfo.push(info);
        vm.upList={};
        DevupService.getVerUp(verInfo).$promise.then(function(data){
            vm.upList=data[0];
            vm.upLists.push(vm.upList)
        })

    }
//单个设备升级数据
    vm.upList=[];
    function verData(ver) {
        var info = {
            devid: ver.devid,
            hardv: ver.hardver,
            softv: ver.softver
        };
        var verInfo = [];
        verInfo.push(info);
        DevupService.getVerUp(verInfo).$promise.then(function(data){
            vm.upList=data;

        })

    }

//确定升级
    vm.upConfirm=function (dev) {
        DevupService.getDevUp(dev).$promise.then(function (data) {
            vm.devUp=data;
            alert("上传成功")
        },function (error) {
            alert("上传失败")
        });
        $('.bgPop,.pop').hide();
    }

/**/







}